package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return  new PageInfo<>(employeeMapper.selectForList(qo));
    }

    @Override
    public void save(Employee employee, Long[] roleIds) {
        // 除了往本表插入员工数据
        employeeMapper.insert(employee);
        // 还要往中间表 employee_role 表插入关系数据
        if (roleIds != null) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public void update(Employee employee, Long[] roleIds) {
        employeeMapper.updateByPrimaryKey(employee);
        // 去中间表 employee_role 这表操作
        // 先删除旧有关系数据
        employeeMapper.deleteRelation(employee.getId());
        // 在插入新的关系数据
        if (roleIds != null) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }

    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        // 先删除旧有关系数据
        employeeMapper.deleteRelation(id);
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> list() {
        return employeeMapper.selectAll();
    }

    @Override
    public Employee login(String username, String password) {
        // session
        return employeeMapper.selectByUsernameAndPassword(username, password);
    }

    @Override
    public void batchDeleteByIds(Long[] ids) {
        employeeMapper.batchDeleteByIds(ids);
    }

    @Override
    public Employee selectByUserName(String username) {
        return employeeMapper.selectByUserName(username);
    }

    @Override
    public Workbook export() {
        List<Employee> employees = employeeMapper.selectAll();
        //创建一个工作文件
        Workbook workbook = new HSSFWorkbook();
        //创建一个工作表
        Sheet sheet = workbook.createSheet();
        //创建每一行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("名字");
        headerRow.createCell(1).setCellValue("email");
        headerRow.createCell(2).setCellValue("age");
        headerRow.createCell(3).setCellValue("admin");

        for(int i = 1;i <= employees.size();i++){
            Row dataRow = sheet.createRow(i);
            Employee employee = employees.get(i - 1);
            dataRow.createCell(0).setCellValue(employee.getName());
            dataRow.createCell(1).setCellValue(employee.getEmail());
            dataRow.createCell(2).setCellValue(Double.valueOf(employee.getAge()));
            dataRow.createCell(3).setCellValue(employee.isAdmin()?"是":"否");
        }
        return workbook;
    }

    @Override
    public void imported(Workbook workbook) {
        Sheet sheet1 = workbook.getSheet("sheet1");
        int lastRowNum = sheet1.getLastRowNum();
        for(int i = 1;i <= lastRowNum;i++){
            Row datarow = sheet1.getRow(i);
            String name = datarow.getCell(0).getStringCellValue();
            String email = datarow.getCell(1).getStringCellValue();
            int age = (int)datarow.getCell(2).getNumericCellValue();
            String adminStr = datarow.getCell(3).getStringCellValue();
            Boolean admin = false;
            if("是"==adminStr){
                admin=true;
            }
            Employee e = new Employee();
            e.setName(name);
            e.setAdmin(admin);
            e.setEmail(email);
            e.setAge(age);
            employeeMapper.insert(e);
        }
    }

    @Override
    public List<Employee> querySellersByRoleSn(String market, String marketManger) {
        return employeeMapper.querySellersByRoleSn(market,marketManger);
    }
}
